package com.menggudf.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.menggudf.commom.CommonUtils;
import com.menggudf.dao.MedicalDBDao;
import com.menggudf.dao.MedicineDao;
import com.menggudf.dao.NewCreatedMedicineDao;
import com.menggudf.entity.MedicalDB;
import com.menggudf.entity.MedicalDBDetail;
import com.menggudf.entity.MedicalTaboo;
import com.menggudf.entity.Medicine;
import com.menggudf.entity.NewCreatedMedicine;
import com.menggudf.model.MedicalDBDetailModel;
import com.menggudf.model.MedicalDBModel;
import com.menggudf.model.MedicalTabooModel;
import com.menggudf.model.MedicineModel;
import com.menggudf.model.NewCreatedMedicineModel;
import com.menggudf.model.ResultModel;
import com.menggudf.service.MedicineService;

@Service
public class MedicineServiceImpl implements MedicineService{
    
    private static final Logger LOGGER = LoggerFactory
            .getLogger(MedicineServiceImpl.class);
    
   /* private final String[] importArr={"药品名称","药品用法","药品频次","是否需要皮试","副作用","禁忌症","药品类型","药品单位剂量","儿童单位剂量","儿童极大值剂量","儿童极小值剂量","成人单位剂量","成人极大值剂量","成人极小值剂量","单位","儿童or成人"};
*/
    private String[] importArr = {"药品名称","药品用法","药品频次","是否需要皮试","副作用","禁忌症","用途","药品类型","药品单位剂量","单位","总数",
                            "最小值剂量","最大值剂量","单位年龄剂量","开始年龄","结束年龄","单位重量剂量","开始重量(KG)","结束重量(KG)","单位倍数","特殊用法"};
    
    @Autowired
    private MedicineDao medicineDao;
    
    @Autowired
    MedicalDBDao medicalDBDao;
    
    @Autowired
    NewCreatedMedicineDao newCreatedMedicineDao;
    
    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel add(
            String name,
            String ingredient,
            String traits,
            String effectType,
            String indications,
            String specification,
            String dosage,
            String adverseReactions,
            String taboo,
            String precautions,
            String price,
            String adminCode) {
        Medicine m = new Medicine();
        try {
            m.setAdverseReactions(adverseReactions);
            m.setCreatedTime(new Date());
            m.setDosage(dosage);
            m.setEffectType(effectType);
            m.setIndications(indications);
            m.setIngredient(ingredient);
            m.setModifyTime(new Date());
            m.setName(name);
            m.setOper(adminCode);
            m.setPrecautions(precautions);
            m.setPrice(price);
            m.setSpecification(specification);
            m.setTaboo(taboo);
            m.setTraits(traits);
            medicineDao.persist(m);
            return ResultModel.success();
        } catch (Exception e) {
            LOGGER.error("add medicine fail",e);
        }
        return ResultModel.fail("add medicine fail", null);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel update(
            String name,
            String ingredient,
            String traits,
            String effectType,
            String indications,
            String specification,
            String dosage,
            String adverseReactions,
            String taboo,
            String precautions,
            String price,
            String adminCode) {
        Medicine m = medicineDao.find(name);
        if(m==null){
            return ResultModel.fail("药品未录入", null);
        }
        if(!adminCode.equalsIgnoreCase(adminCode)){
            return ResultModel.fail("无修改权限", null);
        }
        if(StringUtils.hasText(ingredient)){
            m.setIngredient(ingredient);
        }
        if(StringUtils.hasText(traits)){
            m.setTraits(traits);
        }
        if(StringUtils.hasText(effectType)){
            m.setEffectType(effectType);
        }
        if(StringUtils.hasText(indications)){
            m.setIndications(indications);
        }
        if(StringUtils.hasText(specification)){
            m.setSpecification(specification);
        }
        if(StringUtils.hasText(dosage)){
            m.setDosage(dosage);
        }
        if(StringUtils.hasText(adverseReactions)){
            m.setAdverseReactions(adverseReactions);
        }
        if(StringUtils.hasText(taboo)){
            m.setTaboo(taboo);
        }
        if(StringUtils.hasText(precautions)){
            m.setPrecautions(precautions);
        }
        if(StringUtils.hasText(price)){
            m.setPrice(price);
        }
        try {
            medicineDao.merge(m);
            return ResultModel.success();
        } catch (Exception e) {
            LOGGER.error("update medicine fail ",e);
        }
        
        return ResultModel.fail("update medicine fail",null);
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> get(String name, int pageSize, int offset) {
        List<Medicine> list = medicineDao.getList(name,pageSize,offset);
        long count = medicineDao.getListCount(name);
        List<MedicineModel> mlist = new ArrayList<MedicineModel>();
        for(Medicine m : list){
            MedicineModel mm = new MedicineModel();
            mm.setAdverseReactions(m.getAdverseReactions());
            mm.setCreatedTime(m.getCreatedTime());
            mm.setDosage(m.getDosage());
            mm.setEffectType(m.getEffectType());
            mm.setId(m.getId());
            mm.setIndications(m.getIndications());
            mm.setIngredient(m.getIngredient());
            mm.setModifyTime(m.getModifyTime());
            mm.setName(m.getName());
            mm.setOper(m.getOper());
            mm.setPrecautions(m.getPrecautions());
            mm.setPrice(m.getPrice());
            mm.setSpecification(m.getSpecification());
            mm.setTaboo(m.getTaboo());
            mm.setTraits(m.getTraits());
            mlist.add(mm);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", mlist);
        return map;
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public ResultModel getDetail(String name) {
        Medicine m = medicineDao.getMedicineByName(name);
        if(m==null){
            return ResultModel.fail("药品未录入",null);
        }
        MedicineModel mm = new MedicineModel();
        mm.setAdverseReactions(m.getAdverseReactions());
        mm.setCreatedTime(m.getCreatedTime());
        mm.setDosage(m.getDosage());
        mm.setEffectType(m.getEffectType());
        mm.setId(m.getId());
        mm.setIndications(m.getIndications());
        mm.setIngredient(m.getIngredient());
        mm.setModifyTime(m.getModifyTime());
        mm.setName(m.getName());
        mm.setOper(m.getOper());
        mm.setPrecautions(m.getPrecautions());
        mm.setPrice(m.getPrice());
        mm.setSpecification(m.getSpecification());
        mm.setTaboo(m.getTaboo());
        mm.setTraits(m.getTraits());
        return ResultModel.success("获取成功", mm);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel delete(String name) {
        Medicine m = medicineDao.getMedicineByName(name);
        if(m==null){
            return ResultModel.fail("药品未录入",null);
        }
        try {
            medicineDao.remove(m);
            return ResultModel.success();
        } catch (Exception e) {
            LOGGER.error("delete fail",e);
        }
        return ResultModel.fail();
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public ResultModel getDetailInDB(String name) {
        MedicalDB m = medicalDBDao.findByName(name);
        if(m==null){
            return ResultModel.fail("药品不存在", null);
        }
        MedicalDB mm = new MedicalDB();
        mm.setDetails(m.getDetails());
        mm.setId(m.getId());
        mm.setName(m.getName());
        return ResultModel.success("获取成功", mm);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel createMedicine(String name, String details,Integer skinTest, String usage,String sideEffect, String taboo) {
        MedicalDB mDB = medicalDBDao.findByName(name);
        if(mDB!=null){
            return ResultModel.fail("已经存在", null);
        }
        MedicalDB m = new MedicalDB();
        m.setDetails(details);
        m.setName(name);
        m.setSkinTest(skinTest);
        m.setMedicineUsage(usage);
        m.setSideEffect(sideEffect);
        m.setCreatedTime(new Date());
        try {
            medicalDBDao.persist(m);
            return ResultModel.success("创建成功", null);
        } catch (Exception e) {
            LOGGER.error("createMedicine fail",e);
        }
        return ResultModel.fail("创建失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel deleteMedicine(Integer id) {
        MedicalDB mDB = medicalDBDao.findById(id);
        if(mDB==null){
            return ResultModel.fail("药品不存在", null);
        }
        try {
            medicalDBDao.remove(mDB);
            return ResultModel.success("删除成功", null);
        } catch (Exception e) {
            LOGGER.error("deleteMedicine fail",e);
        }
        return ResultModel.fail("删除失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> getMedicine(String name, int pageSize, int offset) {
        List<MedicalDB> list = medicalDBDao.getMedicineList(name,pageSize,offset);
        long count = medicalDBDao.getMedicineListCount(name);
        List<MedicalDBModel> mlist = new ArrayList<MedicalDBModel>();
        for(MedicalDB m : list){
            MedicalDBModel mdb = new MedicalDBModel();
            mdb.setCreatedTime(m.getCreatedTime());
            mdb.setDetails(m.getDetails());
            mdb.setId(m.getId());
            mdb.setName(m.getName());
            mdb.setMedicineUsage(m.getMedicineUsage());
            mdb.setSideEffect(m.getSideEffect());
            mdb.setSkinTest(m.getSkinTest());
            Set<MedicalDBDetailModel> set = new HashSet<MedicalDBDetailModel>();
            for(MedicalDBDetail md : m.getMedicalDBDetails()){
                MedicalDBDetailModel mdm = new MedicalDBDetailModel();
                mdm.setEndAge(md.getEndAge());
                mdm.setEndWeight(md.getEndWeight());
                mdm.setId(md.getId());
                mdm.setMaxDose(md.getMaxDose());
                mdm.setMinDose(md.getMinDose());
                mdm.setMultiple(md.getMultiple());
                mdm.setSpecialUsage(md.getSpecialUsage());
                mdm.setStartAge(md.getStartAge());
                mdm.setStartWeight(md.getStartWeight());
                mdm.setUnitAge(md.getUnitAge());
                mdm.setUnitWeight(md.getUnitWeight());
                set.add(mdm);
            }
            mdb.setMedicalDBDetails(set);
            
            Set<MedicalTabooModel> set1 = new HashSet<MedicalTabooModel>();
            for(MedicalTaboo mt : m.getMedicalTaboos()){
                MedicalTabooModel mtm = new MedicalTabooModel();
                mtm.setId(mt.getId());
                mtm.setTaboo(mt.getTaboo());
                set1.add(mtm);
            }
            mdb.setMedicalTaboos(set1);
            mlist.add(mdb);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", mlist);
        return map;
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel updateMedicine(Integer id, String details, Integer skinTest, String usage, String sideEffect, String taboo,String name) {
        MedicalDB mDB = medicalDBDao.findById(id);
        if(mDB==null){
            return ResultModel.fail("药品不存在", null);
        }
        try {
            if(!StringUtils.isEmpty(name)){
                mDB.setName(name);
            }
            if(!StringUtils.isEmpty(details)){
                mDB.setDetails(details);
            }
            if(!StringUtils.isEmpty(usage)){
                mDB.setMedicineUsage(usage);
            }
            if(!StringUtils.isEmpty(sideEffect)){
                mDB.setSideEffect(sideEffect);
            }
            if(skinTest!=null){
                mDB.setSkinTest(skinTest);
            }
            medicalDBDao.merge(mDB);
            return ResultModel.success("修改成功", null);
        } catch (Exception e) {
            LOGGER.error("updateMedicine fail",e);
        }
        return ResultModel.fail("修改失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel importExcel(MultipartFile file) {
        FileInputStream fileIn;
        
        CommonsMultipartFile cf= (CommonsMultipartFile)file; 
        DiskFileItem fi = (DiskFileItem)cf.getFileItem(); 
        File f = fi.getStoreLocation();
        MedicalDB m = new MedicalDB();
        Set<MedicalDBDetail> medicalDBDetails = new HashSet<MedicalDBDetail>();
        Set<MedicalTaboo> medicalTaboos = new HashSet<MedicalTaboo>();
        String medicalName = null;
        
        try {
            fileIn = new FileInputStream(f);
          //根据指定的文件输入流导入Excel从而产生Workbook对象  
            Workbook wb0 = WorkbookFactory.create(fileIn);  
            /*Workbook wb0 = new HSSFWorkbook(fileIn);  */
            //获取Excel文档中的第一个表单  
            Sheet sht0 = wb0.getSheetAt(0); 
            int sum = sht0.getLastRowNum();
            //对Sheet中的每一行进行迭代  
            for (Row r : sht0) {
                if(r.getRowNum()==0){
                    for(int i=0;i<importArr.length;i++){
                        if(!r.getCell(i).toString().equalsIgnoreCase(importArr[i])){
                            return ResultModel.fail("importExcel error:["+importArr[i]+"]一栏输入错误", null);
                        }
                    }
                    continue;
                }
                
                //如果当前行的行号（从0开始）未达到2（第三行）则从新循环  
                if(r.getRowNum()<1){  
                    continue;  
                }  
                
                if(r.getRowNum()>sum){
                    break;
                }
                //药品名
                if(r.getCell(0)!=null){
                    medicalName = r.getCell(0).toString();
                    if(medicalName!=null && medicalName!=""){
                        MedicalDB mDB = medicalDBDao.findByName(medicalName);
                        if(mDB==null){
                            m.setName(medicalName);
                        }else{
                            return ResultModel.fail("药品已存在", null);
                        }
                    }
                }
                //用法
                if(r.getCell(1)!=null){
                    String usage = r.getCell(1).toString();
                    if(!StringUtils.isEmpty(usage)){
                        m.setMedicineUsage(usage);
                    }
                }
                //频次
                if(r.getCell(2)!=null){
                    String frequency = r.getCell(2).toString();
                    if(!StringUtils.isEmpty(frequency)){
                        m.setFrequency(frequency);
                    }
                }
                //是否需要皮试
                m.setSkinTest(0);//默认设置
                if(r.getCell(3)!=null){
                    String skinTest = r.getCell(3).toString();
                    if(skinTest.equalsIgnoreCase("是")){
                        m.setSkinTest(1);
                    }else{
                        m.setSkinTest(0);
                    }
                }else{
                    m.setSkinTest(0);
                }
                //副作用
                if(r.getCell(4)!=null){
                    String sideEffect = r.getCell(4).toString();
                    if(!StringUtils.isEmpty(sideEffect)){
                        m.setSideEffect(sideEffect);
                    }
                }
                //禁忌症
                if(r.getCell(5)!=null){
                    String taboo = r.getCell(5).toString();
                    if(!StringUtils.isEmpty(taboo)){
                        MedicalTaboo mt = new MedicalTaboo();
                        mt.setMedicalDB(m);
                        mt.setTaboo(taboo);
                        medicalTaboos.add(mt);
                    }
                }
                //用途
                if(r.getCell(6)!=null){
                    String details = r.getCell(6).toString();
                    if(!StringUtils.isEmpty(details)){
                        m.setDetails(details);
                    }
                }
                //药品类型
                if(r.getCell(7)!=null){
                    String type = r.getCell(7).toString();
                    if(!StringUtils.isEmpty(type)){
                        m.setType(type);
                    }
                }
                //药品单位剂量
                if(r.getCell(8)!=null){
                    Double dose = r.getCell(8).getNumericCellValue();
                    m.setDose(dose);
                }
                //单位
                if(r.getCell(9)!=null){
                    String unit = r.getCell(9).toString();
                    if(!StringUtils.isEmpty(unit)){
                        m.setUnit(unit);
                    }
                }
                //总数
                if(r.getCell(10)!=null){
                    String total = r.getCell(10).toString();
                    if(!StringUtils.isEmpty(total)){
                        m.setTotal(total);
                    }
                }
                
                //最小值剂量
                if(r.getCell(11)!=null){
                    Double minDose = r.getCell(11).getNumericCellValue();
                    MedicalDBDetail detail = new MedicalDBDetail();
                    detail.setMedicalDB(m);
                    detail.setMinDose(minDose);
                    //最大值剂量
                    if(r.getCell(12)!=null){
                        Double maxDose = r.getCell(12).getNumericCellValue();
                        detail.setMaxDose(maxDose);
                    }
                    //单位年龄剂量
                    if(r.getCell(13)!=null){
                        Double unitAge = r.getCell(13).getNumericCellValue();
                        detail.setUnitAge(unitAge);
                    }
                    //开始年龄(天)
                    if(r.getCell(14)!=null){
                        String startAge = r.getCell(14).toString();
                        if(startAge.indexOf("岁")!=-1){
                            startAge = startAge.replace("岁", "").trim();
                            if(CommonUtils.isInteger(startAge)){
                                int age = Integer.parseInt(startAge);
                                age = age*365;
                                detail.setStartAge(age);
                            }else{
                                return ResultModel.fail("开始时间输入格式不对", null);
                            }
                        }else if(startAge.indexOf("月")!=-1){
                            startAge = startAge.replace("月", "").trim();
                            if(CommonUtils.isInteger(startAge)){
                                int age = Integer.parseInt(startAge);
                                age = age*30;
                                detail.setStartAge(age);
                            }else{
                                return ResultModel.fail("开始时间输入格式不对", null);
                            }
                        }else if(startAge.indexOf("天")!=-1){
                            startAge = startAge.replace("天", "").trim();
                            if(CommonUtils.isInteger(startAge)){
                                int age = Integer.parseInt(startAge);
                                detail.setStartAge(age);
                            }else{
                                return ResultModel.fail("开始时间输入格式不对", null);
                            }
                        }else{
                            return ResultModel.fail("开始时间输入格式不对", null);
                        }
                        
                        
                        //结束年龄(天)
                        if(r.getCell(15)!=null){
                            String endAge = r.getCell(15).toString();
                            if(endAge.indexOf("岁")!=-1){
                                endAge = endAge.replace("岁", "").trim();
                                if(CommonUtils.isInteger(endAge)){
                                    int age = Integer.parseInt(endAge);
                                    age = age*365;
                                    detail.setEndAge(age);
                                }else{
                                    return ResultModel.fail("开始时间输入格式不对", null);
                                }
                            }else if(endAge.indexOf("月")!=-1){
                                endAge = endAge.replace("月", "").trim();
                                if(CommonUtils.isInteger(endAge)){
                                    int age = Integer.parseInt(endAge);
                                    age = age*30;
                                    detail.setEndAge(age);
                                }else{
                                    return ResultModel.fail("开始时间输入格式不对", null);
                                }
                            }else if(endAge.indexOf("天")!=-1){
                                endAge = endAge.replace("天", "").trim();
                                if(CommonUtils.isInteger(endAge)){
                                    int age = Integer.parseInt(endAge);
                                    detail.setEndAge(age);
                                }else{
                                    return ResultModel.fail("开始时间输入格式不对", null);
                                }
                            }else{
                                if(r.getCell(14)!=null){
                                    detail.setEndAge(999999);
                                }else{
                                    return ResultModel.fail("开始时间输入格式不对", null);
                                }
                            }
                        }
                        
                    }

                    //单位重量剂量
                    if(r.getCell(16)!=null){
                        Double unitWeight = r.getCell(16).getNumericCellValue();
                        detail.setUnitWeight(unitWeight);
                    }
                    //开始重量(KG)
                    if(r.getCell(17)!=null){
                        Double startWeight = r.getCell(17).getNumericCellValue();
                        detail.setStartWeight(startWeight);
                    }
                    //结束重量(KG)
                    if(r.getCell(18)!=null){
                        Double endWeight = r.getCell(18).getNumericCellValue();
                        detail.setEndWeight(endWeight);
                    }
                    //单位倍数
                    if(r.getCell(19)!=null){
                        Double multiple = r.getCell(19).getNumericCellValue();
                        detail.setMultiple(multiple);
                    }
                    //特殊用法
                    if(r.getCell(20)!=null){
                        String specialUsage = r.getCell(20).toString();
                        if(!StringUtils.isEmpty(specialUsage)){
                            detail.setSpecialUsage(specialUsage);
                        }
                    }
                    medicalDBDetails.add(detail);
                }
                m.setMedicalDBDetails(medicalDBDetails);
                m.setMedicalTaboos(medicalTaboos);
                m.setCreatedTime(new Date());
                medicalDBDao.persist(m);
            }
            fileIn.close();      
            return ResultModel.success("导入成功", null);
        } catch (FileNotFoundException e1) {
            LOGGER.error("FileNotFoundException",e1);
            return ResultModel.fail("导入失败", null);
        } catch (IOException e2) {
            LOGGER.error("IOException",e2);
            return ResultModel.fail("导入失败", null);
        } catch (InvalidFormatException e) {
            LOGGER.error("InvalidFormatException",e);
            return ResultModel.fail("导入失败", null);
        }
    }
    


    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public ResultModel getMedicineInfo(Integer id) {
        MedicalDB m = medicalDBDao.findById(id);
        if(m==null){
            return ResultModel.fail("药品为空", null);
        }
        MedicalDBModel mdb = new MedicalDBModel();
        mdb.setCreatedTime(m.getCreatedTime());
        mdb.setDetails(m.getDetails());
        mdb.setId(m.getId());
        mdb.setName(m.getName());
        mdb.setMedicineUsage(m.getMedicineUsage());
        mdb.setSideEffect(m.getSideEffect());
        mdb.setSkinTest(m.getSkinTest());
        return ResultModel.success("获取成功", mdb);
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public ResultModel getDetailInDB(List<String> names) {
/*        List<MedicalDB> list = medicalDBDao.findByNames(names);
        List<MedicalDBModel> mlist = new ArrayList<MedicalDBModel>();
        for(MedicalDB m : list){
            MedicalDBModel mdb = new MedicalDBModel();
            mdb.setCreatedTime(m.getCreatedTime());
            mdb.setDetails(m.getDetails());
            mdb.setId(m.getId());
            mdb.setName(m.getName());
            mdb.setMedicineUsage(m.getMedicineUsage());
            mdb.setSideEffect(m.getSideEffect());
            mdb.setSkinTest(m.getSkinTest());
            mdb.setTaboo(m.getTaboo());
            mlist.add(mdb);
        }
        return ResultModel.success("获取成功", mlist);*/
        List<MedicalDBModel> mlist = new ArrayList<MedicalDBModel>();
        for(String name : names){
            MedicalDB m = medicalDBDao.findByName(name);
            MedicalDBModel mdb = new MedicalDBModel();
            if(m!=null){
                mdb.setCreatedTime(m.getCreatedTime());
                mdb.setDetails(m.getDetails());
                mdb.setId(m.getId());
                mdb.setName(m.getName());
                mdb.setMedicineUsage(m.getMedicineUsage());
                mdb.setSideEffect(m.getSideEffect());
                mdb.setSkinTest(m.getSkinTest());
                mdb.setType(m.getType());
                
                mdb.setTotal(m.getTotal());
                mdb.setDose(m.getDose());
                mdb.setUnit(m.getUnit());
                mdb.setIsComplete(1);
                mdb.setFrequency(m.getFrequency());
                
                Set<MedicalDBDetailModel> set = new HashSet<MedicalDBDetailModel>();
                for(MedicalDBDetail md : m.getMedicalDBDetails()){
                    MedicalDBDetailModel mdm = new MedicalDBDetailModel();
                    mdm.setEndAge(md.getEndAge());
                    mdm.setEndWeight(md.getEndWeight());
                    mdm.setId(md.getId());
                    mdm.setMaxDose(md.getMaxDose());
                    mdm.setMinDose(md.getMinDose());
                    mdm.setMultiple(md.getMultiple());
                    mdm.setSpecialUsage(md.getSpecialUsage());
                    mdm.setStartAge(md.getStartAge());
                    mdm.setStartWeight(md.getStartWeight());
                    mdm.setUnitAge(md.getUnitAge());
                    mdm.setUnitWeight(md.getUnitWeight());
                    set.add(mdm);
                }
                mdb.setMedicalDBDetails(set);
                
                Set<MedicalTabooModel> set1 = new HashSet<MedicalTabooModel>();
                for(MedicalTaboo mt : m.getMedicalTaboos()){
                    MedicalTabooModel mtm = new MedicalTabooModel();
                    mtm.setId(mt.getId());
                    mtm.setTaboo(mt.getTaboo());
                    set1.add(mtm);
                }
                mdb.setMedicalTaboos(set1);
            }else{
                mdb.setName(name);
                mdb.setIsComplete(0);
            }
            mlist.add(mdb);
        }
        return ResultModel.success("获取成功", mlist);
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> getNewCreatedMedicineList(String name, int pageSize, int offset) {
        List<NewCreatedMedicine> list = newCreatedMedicineDao.getNewCreatedMedicineList(name,pageSize,offset);
        long count = newCreatedMedicineDao.getNewCreatedMedicineListCount(name);
        List<NewCreatedMedicineModel> mlist = new ArrayList<NewCreatedMedicineModel>();
        for(NewCreatedMedicine n : list){
            NewCreatedMedicineModel nm = new NewCreatedMedicineModel();
            nm.setId(n.getId());
            nm.setIsFlag(n.getIsFlag());
            nm.setMedicineName(n.getMedicineName());
            mlist.add(nm);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", mlist);
        return map;
    }
    
}
